clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 012_analysis_Texas, replace


*******************************************************************************
/*				          URPELAINEN & ZHANG 2021	        				 */
*******************************************************************************

/* 

File Name:    	012_analysis_Texas.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/11/2021

Purpose:		

Data Used:      votes_wind_panel_Texas.dta

Program Used:   - reghdfe -

*/


*******************************************************************************
/*                    		 	TABLE A11			   					     */
*******************************************************************************

** Load election district panel
cd "$rootDir/$dataDir/Final"
use election_district_panel_Texas.dta, clear

// Note: Election data from 2004 - 2010; corresponds to stable congressional district boundaries 

** Create instrument and fixed effects
gen t = year - 2004
gen inter = t * mean_wp

egen stateyear_fixed = group(state year)
egen district_fixed = group(state district)

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine +1 )

gen turnout = votes1 + votes2 + votes3 + votes4
gen lnturnout = log(turnout)

local outcome demvotesmajorpercent repvotesmajorpercent incumbvotesmajorpercent turnout lnturnout // outcome variable 
local endogenous cum_capacity_turbine cum_count_turbine cum_lncapacity_turbine cum_lncount_turbine // endogenous variable
local instrument inter // instrument
local admin1_trend stateyear_fixed // geography * time trend
local admin2 district_fixed // panel unit (cluster variable)

foreach y in `outcome' {
	// Create outcome variable label for storing estimates
	local y_name = substr("`y'", 1, 3)
	
	foreach x in `endogenous' {
			// Create endogenous variable label for storing estimates
			tokenize "`x'", parse("_")
			local x_name "`3'"
			di "`x_name'"
			
			// Run IV regression
			reghdfe `y' (`x' = `instrument'), absorb(`admin1_trend' `admin2') ffirst stages(first ols reduced) vce(cluster `admin2') old
			
			// Store IV, first stage, OLS, reduced form estimates
			estimates store `y_name'_`x_name'_iv
			estimates restore reghdfe_first1
			estimates store `y_name'_`x_name'_first
			estimates restore reghdfe_ols
			estimates store `y_name'_`x_name'_ols
			estimates restore reghdfe_reduced
			estimates store `y_name'_`x_name'_reduced
		}
}


*--------------------------- Export LaTeX tables -----------------------------*
cd "$rootDir/$resultDir/Tables"

** Compare OLS and IV estimates
* OLS
esttab dem_capacity_ols dem_count_ols rep_capacity_ols rep_count_ols ///
		inc_capacity_ols inc_count_ols using TableA11.tex, booktabs replace ///
		refcat(cum_capacity_turbine "\emph{Panel A: OLS}", nolabel) ///
		b(%9.3f) se noconstant noobs nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(cum_capacity_turbine "Cumulative capacity (MW)" cum_count_turbine "Cumulative count") varwidth(27) modelwidth(13) ///
		mtitles("Model" "Model" "Model" "Model" "Model" "Model") ///
		mgroups("Democratic Vote" "Republican Vote" "Incumbent Vote", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
* IV
esttab dem_capacity_iv dem_count_iv rep_capacity_iv rep_count_iv ///
		inc_capacity_iv inc_count_iv using TableA11.tex, booktabs append ///
		nomtitles se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(cum_capacity_turbine "Cumulative capacity (MW)" cum_count_turbine "Cumulative count") varwidth(27) modelwidth(13) ///
		refcat(cum_capacity_turbine "\emph{Panel B: IV}", nolabel) ///
		width(\hsize)

		
** Close log file
log close
cd "$rootDir/$logDir"
translate 012_analysis_Texas.smcl 012_analysis_Texas.log, replace

